package com.topgay.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.topgay.domain.Member;
import com.topgay.domain.ValueStrings;
import com.topgay.service.MemberService;
import com.topgay.util.BoardUtilities;
import com.topgay.util.PagingBean;

@Controller
@RequestMapping("/admin/")
public class AdminController {

	@Autowired
	private MemberService service;
	
	@RequestMapping("/login/main.do")
	public String mainPage(){
		return "admin/main";
	}
	
	//로그인
	@RequestMapping(value="/login.do", method=RequestMethod.GET)
	public String loginForm(){
		return "/admin/body/login.jsp";
	}
	
	//로그인 처리
	@RequestMapping(value="/login.do", method=RequestMethod.POST)
	public String login(String id, String password, HttpSession session, ModelMap map) throws SQLException{
		Member member = service.getMemberById(id);
		
		if(member!=null){
			if(password!=null && password.trim().length()!=0){
				if(password.equals(member.getPassword())){
					//로그인 성공
					System.out.println(member);
					session.setAttribute("login_admin_info", member);
					return "admin/main";
				}else{
					//로그인 실패 : 비번 일치안함
					map.addAttribute("error_msg", ValueStrings.getInstance().getErrorMsgLoginPw());
				}
			} else {
				//비밀번호 틀린 경우
				map.addAttribute("error_msg", ValueStrings.getInstance().getErrorMsgLoginPwEmpty());
			}
		} else {
			//ID가 틀린경우, DB에 회원정보값 없음
			map.addAttribute("error_msg", ValueStrings.getInstance().getErrorMsgLoginId());
			
		}
		return "/admin/body/login.jsp";
	}
	
	@RequestMapping(value="/logout.do")
	public String logout(HttpSession session) throws IOException{
		session.invalidate();
//		return "admin/main";
//		return "redirect:/admin/logout_refresh.html";
		return "/admin/body/login.jsp";
	}
	
	//회원정보 목록
	@RequestMapping("/login/member_info.do")
	public String memberInfo(@RequestParam(defaultValue="1")String page, @RequestParam(defaultValue="user")String role, ModelMap model){
		int pageNo = 1;
		try {
			pageNo = Integer.parseInt(page);
		}catch(Exception e){
			pageNo = 1;
		}
		
		model = BoardUtilities.getMemberTitle(model, role);
		
//		1. DAO메소드를 이용해 매개변수로 받은 페이지에 보여줄 게시물들을 ArrayList로 조회
		List<Member> list = service.getMemberList(pageNo, role);
		model.addAttribute("memberlist", list);
		
//		2. PagingBean객체를 생성 ( 필요 정보 : 전체 게시물의 개수, 보고자 하는 페이지 번호)
//		2-1 : DAO의 메소드를 이용해 전체 게시물 수 조회
		int totalContent = service.getTotalContent(role);
		
//		2-2 : PagingBean 객체 생성
		PagingBean pagingBean = new PagingBean(totalContent, pageNo);
		model.addAttribute("pagingBean", pagingBean);
		
		return "admin/member_info";
	}
	
	@RequestMapping("/login/getContent.do")
	public String getContent(String id, @RequestParam(defaultValue="1")String page, ModelMap model){
		
		Member member = service.getMemberContentByNo(id);
		
		model = BoardUtilities.getMemberTitle(model, member.getRole());
		model.addAttribute("member", member);
		model.addAttribute("page", page);
		
		return "admin/member_show_content";
	}
	/*
	//관리자정보 목록
	@RequestMapping("/login/member_admin_info.do")
	public String adminInfo(){
		//TODO
		return "admin/member_admin_info";
	}
	*/
}
